#!/usr/bin/evn python
# -*- coding: UTF-8 -*-
# @File: base_page.py
# @Author: fy
# @date: 2021/10/21 14:42
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from appium.webdriver import Remote


class BasePage:

    def createDriver(self, command_executor='http://127.0.0.1:4723/wd/hub',
                     desired_capabilities=None):
        des_cap = desired_capabilities
        if des_cap is None:
            des_cap = {
                "platformName": "Android",
                "deviceName": "127.0.0.1:62001",
                "appPackage": "com.taobao.taobao",
                "appActivity": "com.taobao.tao.TBMainActivity",
                "printPageSourceOnFindFailure": True,
                "enablePerformanceLogging": True,
                "noReset": True,
                "dontStopAppOnReset": True
            }
        self.driver = Remote(command_executor=command_executor,
                        desired_capabilities=des_cap)
        self.driver.implicitly_wait(30)

    def quit(self):
        self.driver.quit()

    def __init__(self, driver: WebDriver = None):
        self.driver = driver
        return

    def find_element(self, locator, timeout=10):
        eles = self.find_elements(locator, timeout)
        if len(eles) > 0:
            return eles[0]
        return None

    def find_elements(self, locator, timeout=10):
        eles = WebDriverWait(self.driver, timeout).until(lambda x: self.driver.find_elements(*locator))
        return eles

    def find_and_click(self, locator, timeout=10):
        ele = self.find_element(locator)
        if ele is not None:
            ele.click()

    def back(self):
        self.driver.back()

